﻿/*
导航条弹出层js

name 对象的 id 值
divname取决层弹出的位置的对象名称
offsetLeft 定位：左 - 偏移量
offsetTop 定位：顶部 - 偏移量
alignment 对齐方式{-1: left; 0: center; 1: right}
anyIter 显示方式(<=0: 普通样式; >0: 缩放样式)
anyTime 动画时间间隔
staticHover 鼠标覆盖时的样式
popupHover 显示内容鼠标覆盖时的样式
popupLatency 显示内容延时
hideLatency 隐藏内容延时
onInit 初始化
onPopup 显示内容时行为
onHide 隐藏内容时行为
*/
var Divname;
function TFly_Init(name,divname,offsetLeft, offsetTop, alignment, anyIter, anyTime, staticHover, popupHover, popupLatency, hideLatency, onInit, onPopup, onHide) {
 var TFly = document.getElementById(name);
 TFly.DT = document.getElementById(name + "_Popup"); // 显示内容对象
 TFly.F = document.getElementById(name + "_Anim"); // 动画效果对象
 TFly.oX = offsetLeft;
 TFly.oY = offsetTop;
 TFly.alignment = alignment;
 TFly.AnyIter = anyIter;
 TFly.AnyTime = anyTime;
 Divname = divname;

 TFly.popupLatency = popupLatency;
 TFly.hideLatency = hideLatency;
 TFly.onPopup = onPopup;
 TFly.onHide = onHide;

 TFly.onmouseover = function() {
 if (staticHover != '') { // 替换样式，并储存原来的样式
     TFly.className_ = TFly.className;
      TFly.className = staticHover;
    }
 TFly_Popup(TFly, true);
  }

 TFly.onmouseout = function() {
   if (staticHover != '') { // 恢复原来的样式
     TFly.className = TFly.className_;
    }
    TFly_Popup(TFly, false);
  }

 if (document.all) {
 TFly.onactivate = TFly.onmouseover;
 TFly.ondeactivate = TFly.onmouseout;
 } else {
 TFly.onfocus = TFly.onmouseover;
 TFly.onblur = TFly.onmouseout;
 }

 TFly.DT.onmouseover = function() {
 if (popupHover != '') {
     TFly.DT.className_ = TFly.DT.className;
      TFly.DT.className = popupHover;
    }
    TFly_Popup(TFly, true);
  }

 TFly.DT.onmouseout = function() {
   if (popupHover != '') {
 TFly.DT.className = TFly.DT.className_;
    }
   TFly_Popup(TFly, false);
 }

 TFly_CallClientFunction(onInit, TFly);
}

// 调用初始化程序
function TFly_CallClientFunction(name, TFly) {
 if (name != null && name != "")
 eval(name + "(TFly);");
}

// 定位
function TFly_SetPosition(TFly, obj) {
 //obj.style.top = TFly.oY + TFly.offsetHeight + "px";
 //obj.style.left = TFly.oX + (TFly.offsetWidth - obj.offsetWidth ) * (1 + TFly.alignment) / 2 + "px";
 obj.style.left=getObjPosition(document.getElementById(Divname)).x+24;
 obj.style.top = getObjPosition(document.getElementById(Divname)).y-46;
}

function TFly_Popup(TFly, value) {
 if (TFly.timer != null) window.clearTimeout(TFly.timer); // 清除超时

 if (value) { // 判断状态
 if (TFly.status == "on") return;

 // 如果 popup 已经显示，则显示延时为 0
    // 例如从一个菜单移动到另外一个菜单
    var timeout = (TFly.status == "active") ? 0 : TFly.popupLatency;

 if (TFly.AnyIter > 0 && TFly.AnyTime > 0)
 TFly.timer = window.setTimeout(function() {TFly_AnimateStart(TFly, true, TFly.F.curIter);}, timeout);
 else
 TFly.timer = window.setTimeout(function() {TFly_SetVisibility(TFly, true);}, timeout);

 } else {
 if (TFly.status == "off") return;

 var timeout = (TFly.status == "active") ? 0 : TFly.hideLatency;

 if (TFly.AnyIter > 0 && TFly.AnyTime > 0)
 TFly.timer = window.setTimeout(function() {TFly_AnimateStart(TFly, false, TFly.F.curIter);}, timeout);
 else
 TFly.timer = window.setTimeout(function() {TFly_SetVisibility(TFly, false);}, timeout);
 }
}

// 设置弹出内容的可见性
function TFly_SetVisibility(TFly, value) {
 if (value == true) {
 TFly_SetPosition(TFly, TFly.DT);
 TFly.DT.style.visibility = "visible";
 TFly.style.zIndex = "999";
 } else {
 TFly.DT.style.visibility = "hidden";
 TFly.style.zIndex = "0";
 TFly.DT.style.left = "-10000px";
 }
}

function TFly_AnimateStart (TFly, inout, curIter) {
 if (TFly.F.timer != null) window.clearTimeout(TFly.F.timer);

 if (curIter == null) curIter=0;

 if (TFly.status != "active") {
 TFly.status = "active";
 TFly.F.style.visibility = "visible";
 TFly.DT.style.visibility = "hidden";
 }

 if (curIter < 0 || curIter > TFly.AnyIter) {
 TFly.F.curIter = (curIter < 1) ? 0 : TFly.AnyIter;
 TFly.status = (TFly.F.curIter == TFly.AnyIter) ? "on": "off";
 TFly.F.style.visibility = "hidden";
 TFly_SetPosition(TFly, TFly.DT);
 TFly.style.zIndex = (TFly.F.curIter == TFly.AnyIter) ? "999" : "0";
 
 if (TFly.F.curIter == TFly.AnyIter) {
 TFly.status = "on";
 TFly_SetVisibility(TFly, true);
 TFly_CallClientFunction(TFly.onPopup, TFly);
 } else {
 TFly.status = "off";
 TFly_SetVisibility(TFly, false);
 TFly_CallClientFunction(TFly.onHide, TFly);
 }
 } else {
 var per = curIter * (95 / TFly.AnyIter) + 5;
 TFly.F.style.visibility = "hidden";
 TFly.F.style.width = TFly.DT.offsetWidth / 100 * per + "px";
 TFly.F.style.height = TFly.DT.offsetHeight / 100 * per + "px";
 TFly_SetPosition(TFly, TFly.F);
 TFly.F.style.visibility = "visible";
 var timeout = (per == 100 ) ? 0 : TFly.AnyTime - (TFly.AnyTime / (TFly.AnyIter) * (curIter));
 curIter += (inout) ? 1 : -1;
 TFly.F.timer = window.setTimeout(function() {TFly_AnimateStart(TFly, inout, curIter)}, timeout);
 TFly.F.curIter = curIter;
 }
}
//取当前层要显示的位置
function   getObjPosition(e)   
{   
  var   p   =   new   Object();   
  p.x   =   e.offsetLeft;//+e.offsetHeight;    
  p.y   =   e.offsetTop+e.offsetHeight;   
  while   (e.offsetParent)   
  {   
      e   =   e.offsetParent
      p.x   +=   e.offsetLeft;   
      p.y   +=   e.offsetTop;  
  }   
  return   p;  
  alert(p.x+":"+p.y);
}  